const path = require('path')
const Koa = require('koa')
// logger
const logger = require('koa-logger')
// 解析post 请求参数
const bodyParser = require('koa-bodyparser')
// 静态资源加载
const koaStatic = require('koa-static')
// session
const session = require('koa-session-minimal')
const MysqlStore = require('koa-mysql-session')
// view 渲染引擎中间件
const views = require('koa-views')


const routers = require('./routers')
const config = require('./../config/dataBaseConfig')
const cookieConfig = require('./../config/cookieConfig')

const app = new Koa()

// 配置session中间件
app.use(session({
    key: cookieConfig.key,
    store: new MysqlStore(cookieConfig.sessionMysqlConfig),
    cookie: cookieConfig.config
}))

// 配置控制台日志中间件logger
app.use(logger())

// 配置ctx.body解析中间件
app.use(bodyParser())

// 配置静态资源加载中间件
app.use(koaStatic(
    path.join(__dirname, './../resources')
))

// 加载模板引擎
app.use(views(path.join(__dirname, './views'), {
    extension: 'ejs'
}))

// 加载路由中间件
app.use(routers.routes()).use(routers.allowedMethods())

// 监听启动端口
app.listen(config.port)

console.log('http://localhost:' + config.port)